package com.eian.boot.module.system.auth.controller;

import cn.dev33.satoken.annotation.SaIgnore;
import com.eian.boot.common.core.model.CommonResult;
import com.eian.boot.module.system.auth.model.dto.LoginRequest;
import com.eian.boot.module.system.auth.service.AuthService;
import com.eian.boot.module.system.permission.model.dto.PermissionCheckRequest;
import com.eian.boot.module.system.permission.model.vo.PermissionTreeVO;
import com.eian.boot.module.system.role.model.vo.RoleVO;
import com.eian.boot.module.system.user.model.vo.UserLoginResultVO;
import com.eian.boot.security.utils.SessionHelper;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/auth")
@Tag(name = "鉴权模块")
@AllArgsConstructor
public class AuthController {
    private final AuthService authService;

    @SaIgnore
    @Operation(summary = "登录")
    @PostMapping("/login")
    public CommonResult<UserLoginResultVO> login(@Valid @RequestBody LoginRequest params) {
        return CommonResult.ok(authService.login(params));
    }

    @Operation(summary = "用户登出")
    @PostMapping("/logout")
    public CommonResult<String> logout() {
        authService.logout();
        return CommonResult.ok();
    }

    @Operation(summary = "获取当前用户所有权限")
    @GetMapping("/perms")
    public CommonResult<List<String>> getPerms() {
        return CommonResult.ok(authService.getUserAllPerms(SessionHelper.getUserId()));
    }

    @Operation(summary = "检查用户权限有效性")
    @PostMapping("/perms-check")
    public CommonResult<Boolean> permsCheck(@RequestBody PermissionCheckRequest request) {
        return CommonResult.ok(authService.permissionCheck(request));
    }

    @Operation(summary = "获取当前用户菜单树")
    @GetMapping("/menus")
    public CommonResult<List<PermissionTreeVO>> getMenus() {
        return CommonResult.ok(authService.getUserMenuTree(SessionHelper.getUserId()));
    }

    @Operation(summary = "获取当前用户角色列表")
    @GetMapping("/roles")
    public CommonResult<List<RoleVO>> getRoles() {
        return CommonResult.ok(authService.getUserRoleList(SessionHelper.getUserId()));
    }
}
